<?php

namespace catchAdmin\logistics\model;

use catcher\base\CatchModel as Model;
use catcher\CatchAuth;

class Task extends Model
{

    public $name = 'db_task_no';
    protected $deleteTime = 'del_status';
    public function getList()
    {
        $CatchAuth = new CatchAuth();
        $where1['del_status'] = 0;
        //非管理员要商户id
        if($CatchAuth->user()['type'] !== 1 ){
            $where1['tenant_id'] = $CatchAuth->user()['tenant_id'];
        }
        $where = $this->queryParams(true);
        $info = $this->field('id,task_no,warehouse,time,status')
            ->where($where)
            ->where($where1)
            ->order('id', 'desc')
            ->paginate();

        return $info;
    }
    /**
     * 快速搜索
     * @param $timeFormat boolean  false:不需要strtotime转化时间戳  true：需要转化时间戳
     */
    public function queryParams($timeFormat)
    {
        $requestParams = \request()->param();
        unset($requestParams['page']);
        unset($requestParams['pageNum']);
        unset($requestParams['limit']);

        if (empty($requestParams)) {
            return null;
        }
        $where = [];

        foreach ($requestParams as $field => $value) {
            if ($value === "") {
                continue;
            }
            // 区间范围 start_数据库字段 & end_数据库字段
            $startPos = strpos($field, 'start_');
            if ($startPos === 0) {
                $value = $timeFormat ? strtotime($value) : $value;
                $where[] = [str_replace('start_','', $field), '>=',$value];
                continue;

            }
            $endPos = strpos($field, 'end_');
            if ($endPos === 0) {
                $value = $timeFormat ? strtotime($value) : $value;
                $where[] = [str_replace('end_','', $field), '<=',$value];
                continue;
            }
            //模糊查询 like_数据库字段
            $likePos = strpos($field, 'like_');
            if ($likePos === 0) {
                $where[] = [str_replace('like_','', $field), 'like', "%$value%"];
                continue;
            }
            //=查询
            $where[] = [$field,'=',$value];
        }
        return $where;
    }

}